#pragma once
#include"Singleton.h"//这里也要实现单例
#include<boost/asio.hpp>
#include<vector>


class AsioIOServicePool:public Singleton<AsioIOServicePool>
{
	friend class Singleton<AsioIOServicePool>;
public:
	using IOService = boost::asio::io_context;
	using Work = boost::asio::io_context::work;//asio的work是为了防止io_context在没有被注册事件的时候退出,
	//io_c.run()并不是阻塞的,之前好像阻塞在那里是因为给它注册了acceptor的监听事件
	//正常情况下没有给他绑定事件的时候直接run会退出,用work防止它退出
	using WorkPtr = std::unique_ptr<Work>;//就是希望work不要被改变,用独占指针
	~AsioIOServicePool();
	AsioIOServicePool(const AsioIOServicePool&) = delete;
	AsioIOServicePool& operator = (const AsioIOServicePool&) = delete;
	//轮询方式返回一个io_c
	boost::asio::io_context& GetIOService();
	void Stop();

private:
	AsioIOServicePool(std::size_t size = std::thread::hardware_concurrency());
	std::vector<IOService> _ioServices;
	std::vector<WorkPtr> _works;
	std::vector<thread> _threads;
	std::size_t _nextIOService;//下一次返回io_c的坐标
};

